package net.qhzw.vip.qiyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.qhzw.vip.qiyu.entity.SysVideos;
import net.qhzw.vip.qiyu.handler.MyException;
import net.qhzw.vip.qiyu.mapper.SysVideosMapper;
import net.qhzw.vip.qiyu.service.SysVideosService;
import net.qhzw.vip.qiyu.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class SysVideosServiceImpl extends ServiceImpl<SysVideosMapper, SysVideos> implements SysVideosService {


    @Autowired
    private RedisUtils redisUtils;

    @Override
    public Page<SysVideos> findByPage(Page<SysVideos> page, String title, Byte status) {
        Page<SysVideos> sysVideosPage = page(page, new LambdaQueryWrapper<SysVideos>()
                .like(!StringUtils.isEmpty(title), SysVideos::getTitle, title)
                .eq(status != null, SysVideos::getStatus, status));
        List<SysVideos> records = sysVideosPage.getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            for (SysVideos record : records) {
                Long playVolume = (Long) redisUtils.getCacheObject("sysVideos:playVolume:" + record.getId());
                playVolume = playVolume == null ? 1 : playVolume;
                record.setPlayVolume(playVolume);
            }
        }
        return sysVideosPage;
    }

    @Override
    public SysVideos getSysVideos(Long id) {
        SysVideos sysVideos = getById(id);
        if (sysVideos == null) {
            throw new MyException("查询失败,参数错误");
        }
        Long playVolume = (Long) redisUtils.getCacheObject("sysVideos:playVolume:" + id);
        playVolume = playVolume == null ? 1 : playVolume;
        redisUtils.setCacheObject("sysVideos:playVolume:" + id, ++playVolume);
        sysVideos.setPlayVolume(playVolume);
        return sysVideos;
    }
}
